﻿using mozhi.smarterp.Finance.Banks.Repository;
using mozhi.smarterp.Finance.Enums;
using mozhi.smarterp.Finance.Treasuries.Repositories;
using Mozhi.Abp.Domain.BasicData.Repositories;
using System;
using System.Threading.Tasks;
using Volo.Abp.Guids;

namespace mozhi.smarterp.Finance.Treasuries.Services
{
    public class CollectionManager : DocumentDomainService<Collection, Guid>
    {
        private IGuidGenerator _guidGenerator;
        private IOrganizationRepository _organizationRepository;
        private ISupplierRepository _supplierRepository;
        private ICustomerRepository _customerRepository;
        private IEmployeeRepository _employeeRepository;
        private IBankAccountRepository _bankAccountRepository;
        private ICurrencyRepository _currencyRepository;

        public CollectionManager(ICollectionRepository repository, IGuidGenerator guidGenerator, INumberGeneratorRepository numberGeneratorRepository, ICurrencyRepository currencyRepository,
            IOrganizationRepository organizationRepository, ISupplierRepository supplierRepository, IEmployeeRepository employeeRepository, ICustomerRepository customerRepository, IBankAccountRepository bankAccountRepository)
            : base(repository, numberGeneratorRepository, DocumentType.Collection)
        {
            _guidGenerator = guidGenerator;
            _organizationRepository = organizationRepository;
            _supplierRepository = supplierRepository;
            _customerRepository = customerRepository;
            _employeeRepository = employeeRepository;
            _bankAccountRepository = bankAccountRepository;
            _currencyRepository = currencyRepository;
        }

        public async Task<Collection> GetAsync(Guid? id, DateTime date, Guid? bankAccountId, TradingPartnerType tradingPartnerType, Guid? supplierId, Guid? employeeId, Guid? customerId,
            Guid currencyId, decimal exchangeRate, bool isAdvanced, bool isRefund, string comment)
        {
            var supplier = supplierId.HasValue ? await _supplierRepository.GetAsync(supplierId.Value) : null;
            var cusomter = customerId.HasValue ? await _customerRepository.GetAsync(customerId.Value) : null;
            var employee = employeeId.HasValue ? await _employeeRepository.GetAsync(customerId.Value) : null;
            var currency = await _currencyRepository.GetAsync(currencyId);

            currency.SetExchangeRate(exchangeRate);

            Collection collection;

            if (id.HasValue)
            {
                collection = await _repository.GetAsync(id.Value);

                collection.Change(date, bankAccountId, tradingPartnerType, supplier, employee, cusomter, currency, isAdvanced, isRefund, comment, collection.Status);
            }
            else
            {
                var number = await GetNextNumberAsync();

                collection = new Collection(_guidGenerator.Create(), number, date, bankAccountId, tradingPartnerType, supplier, employee, cusomter, currency, isAdvanced, isRefund, comment, DocumentStatusType.Creation);
            }

            return collection;
        }


        public async Task<CollectionItem> GetItemAsync(Guid? id, decimal amountFr, string remark)
        {
            id = id ?? _guidGenerator.Create();

            var item = new CollectionItem(id.Value, null, null, amountFr, null, remark);

            return item;
        }
    }
}
